<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
<title>添加出库产品</title>
<meta name="decorator" content="default" />
<script type="text/javascript">
	var URI = "";
	var maxAmount;
	function fillData(item, popID) {
		$("#product-id").val(item.attr("id"));
		$("#product-name").val(item.attr("name"));
		$("#product-category").val(item.attr("category"));
		$("#product-spec").val(item.attr("spec"));
		$("#product-retail_price").val(item.attr("price"));
		$("#product-real-amount").val(item.attr("amount"));
		$("#product-amount").val(item.attr("amount"));
		maxAmount = item.attr("amount");
		$(popID).popup("close");
	}

	function checkStoreProduct() {
		var id = document.getElementById("storeProduct-id").value;
		if (id < 1) {
			showTip("请先选择产品");
			return false;
		}
		var count = document.getElementById("storeProduct-count").value;
		if (count < 1) {
			showTip("请输入产品数量");
			return false;
		}
		return true;
	}

	function checkSystemProduct() {
		var id = document.getElementById("systemProduct-id").value;
		if (id < 1) {
			showTip("请先选择产品");
			return false;
		}
		var count = document.getElementById("systemProductCount").value;
		if (count < 1) {
			showTip("请输入产品数量");
			return false;
		}
		return true;
	}
	function checkAddCondition() {
		if (!$("#product-id").val() && !$("#storeProduct-id").val()) {
			showTip("请选择要入库的产品");
			return false;
		}
		var left = document.getElementById("product-real-amount").value;
		var add = document.getElementById("product-amount").value;
		if (parseInt(left) < parseInt(add)) {
			showTip("库存不足，请调整数量，实际库存为: " + left);
			return false;
		}
		return true;
	}

	$().ready(function() {
		$("#selectSystemProductEvent ul li").delegate("a", "click", function() {
			fillData($(this), "#selectSystemProduct");
		});

		$("#selectStoreProductEvent ul li").delegate("a", "click", function() {
			fillData($(this), "#selectStoreProduct");
		});
	});
	function page(n, s) {
		$("#pageNo").val(n);
		$("#pageSize").val(s);
		$("#searchForm").submit();
		return false;
	}
</script>
</head>
<body>
	<div class="container">
		<div style="text-align: center;">
			<c:if test="${!outProduct.editing}">
				<h3>添加产品</h3>
			</c:if>
			<c:if test="${outProduct.editing}">
				<h3>编辑产品</h3>
			</c:if>
		</div>
		<form:form id="outProductForm" class="form-input"
			action="${ctx}/inventory/out/product/${outProduct.editing?'edit':'add'}"
			modelAttribute="outProduct">
			<div>
				<c:if test="${!outProduct.editing}">
					<a href="#selectSystemProduct" data-rel="popup" data-role="button"
						data-inline="true" data-position-to="window" data-transition="pop">系统产品</a>
					<a href="#selectStoreProduct" data-rel="popup" data-role="button"
						data-inline="true" data-position-to="window" data-transition="pop">店面自营产品</a>
				</c:if>
				<form:hidden path="product.id" id="product-id" />
				<form:hidden path="product.product_type" id="product-type" />
				<div data-role="fieldcontain">
					<label>产品分类</label>
					<form:input type="text" id="product-category" readonly="true"
						path="product.product.productCategory.name" />
				</div>

				<div data-role="fieldcontain">
					<label>产品名称</label>
					<form:input type="text" id="product-name" readonly="true"
						path="product.product.name" />

				</div>
				<div data-role="fieldcontain">
					<label>产品规格</label>
					<form:input path="product.product.spec" type="text"
						id="product-spec" readonly="true" />
				</div>
				<div data-role="fieldcontain">
					<label>零售价</label>
					<form:input path="product.product.retail_price" type="text"
						id="product-retail_price" readonly="true" />
				</div>

				<div data-role="fieldcontain">
					<input type="hidden" id="product-real-amount"
						name="product-real-amount" /> <label>出货数量</label>
					<form:input path="amount" type="number" id="product-amount" />
				</div>
			</div>
			<div class="icon-buttons">
				<a data-role="button" data-inline="true"  data-icon="back" data-iconpos="notext"
					href="${ctx}/inventory/out/product/page">返回</a>
				<c:if test="${outProduct.editing}">
					<input type="submit" value="保存"  data-icon="check" data-iconpos="notext" data-inline="true"
						onclick="return checkAddCondition()" />
				</c:if>
				<c:if test="${!outProduct.editing}">
					<input type="submit" value="添加"  data-icon="check" data-iconpos="notext" data-inline="true"
						onclick="return checkAddCondition()" />
				</c:if>
			
			</div>
		</form:form>
		<c:if test="${!outProduct.editing}">
			<div data-role="popup" id="selectSystemProduct">
				<div data-role="header">
					<h1>选择产品</h1>
				</div>
				<div data-role="content" style="overflow:scroll;height:25em;width: 20em;"
					id="selectSystemProductEvent">
					<ul data-role="listview" id="productsList" data-filter="true" data-filter-placeholder="搜索">
						<c:forEach items="${inventoryOut.systemProductList}" var="data">
							<li><a name="${data.product.name}" amount="${data.amount}"
								category="${data.product.productCategory.name}"
								spec="${data.product.spec}" id="${data.id}"
								price="${data.product.retail_price}" href="#">${data.product.name}</a></li>
						</c:forEach>
					</ul>
				</div>
			</div>
			<div data-role="popup" id="selectStoreProduct">
				<div data-role="header">
					<h1>选择店面自营产品</h1>
				</div>
				<div data-role="content" style="overflow:scroll;height:25em;width: 20em;"
					id="selectStoreProductEvent">
					<ul data-role="listview" id="productsList" data-filter="true" data-filter-placeholder="搜索">
						<c:forEach items="${inventoryOut.storeProductList}" var="data">
							<li><a name="${data.storeSelfProduct.name}"
								amount="${data.amount}"
								category="${data.storeSelfProduct.storeSelfProductCategory.name}"
								spec="${data.storeSelfProduct.spec}" id="${data.id}"
								price="${data.storeSelfProduct.retail_price}" href="#">${data.storeSelfProduct.name}</a></li>
						</c:forEach>
					</ul>
				</div>
			</div>
		</c:if>
	</div>
</body>
</html>
